<?php
namespace app\tools;

use Yii;

class Excel
{
    public static function readExcel($file_path, $file_type) {
        require_once '../vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
        if ($file_type == 'xls') {
            $excelReader = \PHPExcel_IOFactory::createReader('Excel5');
        } else {//后缀是xlsx
            $excelReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        $phpexcel = $excelReader->load($file_path)->getSheet(0);//载入文件并获取第一个sheet
        $total_line = $phpexcel->getHighestRow();//最大行，如145
        $total_column = $phpexcel->getHighestColumn();//最大列，如AL列

        $data = array();
        for ($row = 0; $row < $total_line; $row++) {
            $all_empty = true;
            $column_count = 22;//每行的长度
            for ($column = 'A'; $column != $total_column && $column_count > 0; $column++, $column_count--) {//'A'++ 等于 'B'，'A'+1 等于 1
                $value = trim($phpexcel->getCell($column . ($row + 1))->getValue());
                $data[$row][] = $value;//获取单元格的值
                if ($value !== '') {
                    $all_empty = false;
                }
            }
            $value = trim($phpexcel->getCell($column . ($row + 1))->getValue());
            $data[$row][] = $value;//获取单元格的值
            if ($value !== '') {
                $all_empty = false;
            }
            if ($all_empty) {
                unset($data[$row]);
            }
        }
        return $data;
    }

    public static function fetchFile($file_path) {
        $file = file_get_contents($file_path);
        $local_path = 'user_'.Yii::$app->user->id.'_'.time().'.xls';
        file_put_contents($local_path, $file);
        return $local_path;
    }

    public static function convertToHoursMins($join_time,$second) {
      $day = floor($second/(3600*24));
    	$second = $second%(3600*24);
    	$hour = floor($second/3600);
    	$second = $second%3600;
    	$minute = floor($second/60);
    	$second = $second%60;
    	if ($second > 0) {
    	   $minute += 1;
      } else if ($join_time > 0) {
        $minute = 1;
      }
    	return $hour.'时'.$minute.'分';
    }
}
